package com.hnyfkj.jyindustry.biz.kehu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.hnyfkj.jyindustry.biz.chongzhilog.dao.ChongzhiLogDao;
import com.hnyfkj.jyindustry.biz.chongzhilog.entity.ChongzhiLog;
import com.hnyfkj.jyindustry.biz.kehu.dao.UserDao;
import com.hnyfkj.jyindustry.biz.kehu.dto.UserInsertDto;
import com.hnyfkj.jyindustry.biz.kehu.dto.UserQueryDto;
import com.hnyfkj.jyindustry.biz.kehu.dto.UserUpdateDto;
import com.hnyfkj.jyindustry.biz.kehu.entity.User;
import com.hnyfkj.jyindustry.biz.kehu.service.UserService;
import com.hnyfkj.jyindustry.biz.xiaofei.dao.XiaofeiLogDao;
import com.hnyfkj.jyindustry.biz.xiaofei.entity.XiaofeiLog;
import com.hnyfkj.jyindustry.common.dto.PageReturnDto;
import com.hnyfkj.jyindustry.common.dto.RespDto;
import com.hnyfkj.jyindustry.common.utils.ShiroUtils;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;

import java.util.Arrays;

@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {


    @Autowired
    private UserDao userDao;

    @Autowired
    private XiaofeiLogDao xiaofeiLogDao;

    @Autowired
    private ChongzhiLogDao chongzhiLogDao;


    @Override
    public PageReturnDto<User> queryPage(UserQueryDto queryDto) {
        PageInfo<User> page =
                PageHelper.startPage(queryDto.getPage(), queryDto.getLimit()).doSelectPageInfo(() -> userDao.selectListByQuery(queryDto));
        return PageReturnDto.pageOf(page);
    }

    @Override
    public int insert(UserInsertDto userInsertDto) {
        final User user = new User();
        BeanUtils.copyProperties(userInsertDto, user);
        //第一次开卡,存储充值记录
        ChongzhiLog chongzhiLog = new ChongzhiLog();
        chongzhiLog.setPhone(user.getPhone());
        chongzhiLog.setName(user.getMemberName());
        chongzhiLog.setChongzhi(userInsertDto.getChongzhi() + "元");
        chongzhiLogDao.insert(chongzhiLog);
        user.setShopId(ShiroUtils.getSessionAttribute("memberId").toString());
        return userDao.insert(user);
    }

    @Override
    public int update(UserUpdateDto userUpdateDto) {
        final User user = new User();
        BeanUtils.copyProperties(userUpdateDto, user);
        return userDao.updateById(user);
    }

    @Override
    public int delete(String ids) {
        String[] id = ids.split(",");
        return userDao.deleteBatchIds(Arrays.asList(id));
    }

    @Override
    public RespDto<String> jianfa(String ids) {
        User user = userDao.selectById(ids);
        if (user.getJianfacount() == 0) {
            return RespDto.succ("剪发次数不足,请充值!");
        }
        user.setJianfacount(user.getJianfacount() - 1);
        userDao.updateById(user);

        //消费金额记录
        XiaofeiLog xiaofeiLog = new XiaofeiLog();
        xiaofeiLog.setPhone(user.getPhone());
        xiaofeiLog.setName(user.getMemberName());
        xiaofeiLog.setXiaofei("剪发消费1次");
        xiaofeiLogDao.insert(xiaofeiLog);
        return RespDto.succ("消费成功!");
    }

    @Override
    public RespDto<String> ganxi(String ids) {
        User user = userDao.selectById(ids);
        if (user.getGanxicount() == 0) {
            return RespDto.succ("干洗次数不足,请充值!");
        }
        user.setGanxicount(user.getGanxicount() - 1);
        userDao.updateById(user);

        //消费金额记录
        XiaofeiLog xiaofeiLog = new XiaofeiLog();
        xiaofeiLog.setPhone(user.getPhone());
        xiaofeiLog.setName(user.getMemberName());
        xiaofeiLog.setXiaofei("干洗消费1次");
        xiaofeiLogDao.insert(xiaofeiLog);
        return RespDto.succ("消费成功!");
    }

    @Override
    public RespDto<String> xifa(String ids) {
        User user = userDao.selectById(ids);
        if (user.getXifacount() == 0) {
            return RespDto.succ("洗发次数不足,请充值!");
        }
        user.setXifacount(user.getXifacount() - 1);
        userDao.updateById(user);
        //消费金额记录
        XiaofeiLog xiaofeiLog = new XiaofeiLog();
        xiaofeiLog.setPhone(user.getPhone());
        xiaofeiLog.setName(user.getMemberName());
        xiaofeiLog.setXiaofei("洗发消费1次");
        xiaofeiLogDao.insert(xiaofeiLog);
        return RespDto.succ("消费成功!");
    }

}